import java.awt.Point;
import java.util.ArrayList;

/**
 * This class describes a Graph as a set of vertices and a collection of Edge.
 * The provide all the methods in order to create a correct graph given a set of 
 * Edge an a set of vertices.
 * A vertex is identified as an integer number,while an Edge is identified by a Point(x,y)
 * where x and y consist on vertex.
 * 
 * @author Scinetti,Trombini,Garcia Fernandez
 *
 */

public class Graph{
	
	public ArrayList<Integer> V;
	public ArrayList<Point> E;
	
	public Graph(){
		this.V = new ArrayList<Integer>();
		this.E = new ArrayList<Point>();
	}
	
	/**
	 * Provide to create a graph G = (V,E) given a set of Verticies
	 * @param v is the set of verticies whose we want to create the graph G = (V,E)
	 */
	
	public void addVertex(ArrayList<Integer> v){
	 this.V = v;
	 
	 for(int i=0;i < V.size() ;i++){
		 addEdge(V.get(i)+"",V.get((i+1)%V.size())+"");
	 }
	}
	
	/**
	 * Given a set of two vertices this method allow to create a new edge connection
	 * @param from starting vertex of new desired edge
	 * @param to ending vertex of new desired edge
	 */
	
	public void addEdge(String from,String to){
		Point toAdd = new Point(Integer.parseInt(from),Integer.parseInt(to));
		this.E.add(toAdd);
	}
	
	/**
	 * Method that Given a set of edges,provide to extract 
	 * all the vertices in the Graph
	 */
	
	public void extractVertex(){
		ArrayList<Point> edges = this.E;
		for (Point edge : edges) {
			if(!V.contains(edge.x)){
				V.add(edge.x);
			}
			if(!V.contains(edge.y)){
				V.add(edge.y);
			}
			
		}
		
	}
	
	/**
	 * Method that allow to print the graph G in a correct format
	 */
	@Override
	public String toString() {
		String toPrint = "{";
		for (Integer vertex : V) {
			toPrint+= vertex +",";
		}
		toPrint+="}\n{";
		for (Point edge : E) {
			toPrint+= "{"+edge.getX() +"," + edge.getY()+"},";
	
		
		}
		return toPrint+="}";	
	}
	
	
}
